package com.feng.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feng.chat.dto.Result;
import com.feng.chat.dto.TagListDTO;
import com.feng.chat.entity.Article;
import com.feng.chat.entity.ArticleTag;
import com.feng.chat.mapper.ArticleMapper;
import com.feng.chat.mapper.ArticleTagMapper;
import com.feng.chat.service.IArticleTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.chat.vo.TagVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 笔记标签表 服务实现类
 * </p>
 *
 * @author dyf
 * @since 2022-05-31
 */
@Service
public class ArticleTagServiceImpl extends ServiceImpl<ArticleTagMapper, ArticleTag> implements IArticleTagService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleTagMapper articleTagMapper;

    /**
     * 获取笔记标签列表
     *
     * @param uid
     * @return
     */
    @Override
    public Result tagList(Integer uid) {
        QueryWrapper<ArticleTag> articleTagQueryWrapper = new QueryWrapper<>();
        articleTagQueryWrapper.eq("user_id", uid).eq("is_delete", 0).orderByAsc("sort");
        List<ArticleTag> articleTags = articleTagMapper.selectList(articleTagQueryWrapper);
        List<TagListDTO> tagListDTOS = new ArrayList<>();
        tagListDTOS = articleTags.stream().map(item -> {
            List<Article> tagArticle = articleMapper.getTagArticle(item.getId(), item.getUserId());
            TagListDTO tagListDTO = new TagListDTO();
            tagListDTO.setId(item.getId());
            tagListDTO.setTag_name(item.getTagName());
            tagListDTO.setCount(tagArticle.size());
            return tagListDTO;
        }).collect(Collectors.toList());

        Map<String, List<TagListDTO>> map = new HashMap<>();
        map.put("tags", tagListDTOS);
        return Result.success(map);
    }

    /**
     * 编辑标签
     *
     * @param uid
     * @param tag_id
     * @param tag_name
     * @return
     */
    @Override
    public Result tagEditor(Integer uid, Integer tag_id, String tag_name) {
        Map<String, Integer> map = new HashMap<>();
        if (tag_id == 0) {//新建标签
            QueryWrapper<ArticleTag> articleTagQueryWrapper = new QueryWrapper<>();
            articleTagQueryWrapper.eq("user_id", uid).eq("tag_name", tag_name);
            ArticleTag articleTag = articleTagMapper.selectOne(articleTagQueryWrapper);
            if (articleTag != null) {
                if (articleTag.getIsDelete() == 1) {
                    articleTag.setIsDelete(0);
                    articleTagMapper.updateById(articleTag);

                    map.put("id", articleTag.getId());
                    return Result.success(map);
                }
                return Result.fail("该标签已存在！！！");
            }
            ArticleTag tag = new ArticleTag();
            tag.setTagName(tag_name);
            tag.setUserId(uid);
            articleTagMapper.insert(tag);
            map.put("id", tag.getId());

            return Result.success(map);
        } else {//编辑标签
            ArticleTag articleTag = articleTagMapper.selectById(tag_id);
            articleTag.setTagName(tag_name);
            articleTagMapper.updateById(articleTag);

            map.put("id", articleTag.getId());
            return Result.success(map);
        }
    }

    /**
     * 删除标签
     * @param uid
     * @param tag_id
     * @return
     */
    @Override
    public Result tagDelete(Integer uid, Integer tag_id) {
        List<Article> articles = articleMapper.getTagArticle(tag_id,uid);
        if (articles.size()>0){
            return Result.fail("当前标签下含有笔记，无法删除！");
        }
        ArticleTag articleTag = articleTagMapper.selectById(tag_id);
        articleTag.setIsDelete(1);
        articleTagMapper.updateById(articleTag);
        return Result.success();
    }

    /**
     * 修改笔记标签
     * @param tagVO
     * @return
     */
    @Override
    public Result articleTag(TagVO tagVO) {
        Article article = articleMapper.selectById(tagVO.getArticle_id());
        String tags = String.join(",", tagVO.getTags());
        article.setTagsId(tags);
        articleMapper.updateById(article);
        return Result.success();
    }
}
